﻿using System;
using System.Windows;
using jSLCore.Client.Presentation.ApplicationContainer;
using jSLCore.Client.Presentation.AuthorizeService;
using jSLCore.Client.Presentation.BaseControl;
using jSLCore.Client.Presentation.BaseControl.DialogMessage;

namespace jSLCore.Client.Presentation.Authorization
{
    /// <summary>
    /// Form for viewing and editing current user information
    /// </summary>
    public partial class AccountDetail : DialogControl
    {
        private readonly IPasswordManager _passwordManager;

        public AccountDetail(IPasswordManager passwordManager)
        {
            _passwordManager = passwordManager;
            InitializeComponent();
        }

        private void changePwd_Clicked(object sender, RoutedEventArgs e)
        {
            var dlg1 = new jAskForStringBox("Введите старый пароль:",null,false);
            dlg1.ShowDialog(delegate(object o, object oldPwd)
                                {
                                    var dlg2 = new jAskForStringBox("Введите новый пароль:", null, false);
                                    dlg2.ShowDialog(delegate(object sender1, object newPwd)
                                                        {
                                                            var dlg3 = new jAskForStringBox("Повторите новый пароль:", null, false);
                                                            dlg3.ShowDialog(delegate(object o1, object newPwd2)
                                                                                {
                                                                                    if (newPwd.ToString() != newPwd2.ToString())
                                                                                    {
                                                                                        ErrorBox.ShowDialog("Новый пароль и его повторный ввод не совпадают, пожалуйста повторите ввод");
                                                                                        return;
                                                                                    }
                                                                                    DoWork("Смена пароля...",(sender2, args) => _passwordManager.ChangePassword((string)oldPwd, (string)newPwd),(o2, eventArgs) =>
                                                                                                                         {
                                                                                                                             if(eventArgs.Error==null)
                                                                                                                             {
                                                                                                                                 InfoBox.ShowDialog("Пароль успешно изменен");
                                                                                                                                 InvokeDataSelected();
                                                                                                                             }
                                                                                                                         });
                                                                                },this );
                                                        },this );
                                },this );

        }

        public override void LoadInitialData()
        {
            DoWork("Загрузка информации о пользователе",(sender, args) =>
                                                            {
                                                                args.Result = _passwordManager.GetCurrentUser();
                                                            },(o, eventArgs) =>
                                                                                  {
                                                                                      if(eventArgs.Error!=null)
                                                                                      {
                                                                                          InvokeCancelSelected();
                                                                                          return;
                                                                                      }

                                                                                      SetDataContext((User) eventArgs.Result);
                                                                                  });
        }


        public void SetDataContext(User user)
        {
            User = user;
        }
        public User User
        {
            get { return (User) DataContext; }
            set { DataContext = value; }
        }

        public override bool IsOkVisible
        {
            get { return false; }
        }

        public override bool IsStandartOkCancelEnabled
        {
            get { return true; }
        }

        public override object CancelContent
        {
            get { return "Закрыть"; }
        }

        public override bool AutoSize
        {
            get { return true; }
        }
    }
}
